// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/init/init.h>
#include <gtest/gtest.h>

#include <pollux/connectors/hive/storage_adapters/gcs/register_gcs_filesystem.h>
#include <pollux/connectors/hive/storage_adapters/gcs/tests/GcsEmulator.h>
#include <pollux/connectors/hive/storage_adapters/test_common/InsertTest.h>

using namespace kumo::pollux::exec::test;

namespace kumo::pollux::filesystems {
namespace {

class GcsInsertTest : public testing::Test, public test::InsertTest {
 protected:
  static void SetUpTestSuite() {
    registerGcsFileSystem();
    memory::MemoryManager::testingSetInstance({});
  }

  void SetUp() override {
    connector::registerConnectorFactory(
        std::make_shared<connector::hive::HiveConnectorFactory>());
    emulator_ = std::make_shared<GcsEmulator>();
    emulator_->bootstrap();
    auto hiveConnector =
        connector::getConnectorFactory(
            connector::hive::HiveConnectorFactory::kHiveConnectorName)
            ->newConnector(
                exec::test::kHiveConnectorId,
                emulator_->hiveConfig(),
                ioExecutor_.get());
    connector::registerConnector(hiveConnector);
    parquet::registerParquetReaderFactory();
    parquet::registerParquetWriterFactory();
    ioExecutor_ = std::make_unique<melon::IOThreadPoolExecutor>(3);
  }

  void TearDown() override {
    parquet::unregisterParquetReaderFactory();
    parquet::unregisterParquetWriterFactory();
    connector::unregisterConnectorFactory(
        connector::hive::HiveConnectorFactory::kHiveConnectorName);
    connector::unregisterConnector(exec::test::kHiveConnectorId);
  }

  std::shared_ptr<GcsEmulator> emulator_;
  std::unique_ptr<melon::IOThreadPoolExecutor> ioExecutor_;
};
} // namespace

TEST_F(GcsInsertTest, gcsInsertTest) {
  const int64_t kExpectedRows = 1'000;
  const auto gcsBucket = gcsURI(emulator_->preexistingBucketName(), "");
  runInsertTest(gcsBucket, kExpectedRows, pool());
}
} // namespace kumo::pollux::filesystems

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  melon::Init init{&argc, &argv, false};
  return RUN_ALL_TESTS();
}
